<?

/**
 * $Id: AuthDAL.mod.php 708 2008-06-26 13:48:43Z pawy $
 * Klasa autoryzacji usera
 *
 */
class AuthDAL {
    /**
     * Pusty konstruktor
     *
     */
    public function __construct() {

    }

    /**
     * Sprawdza uzytkownika i haslo, loguje usera , zapisuje obiekt klasy User do sesji i zwraca 1/0
     *
     * @param string $login
     * @param string $password
     * @return boolean
     */
    public static function Login($login, $password) {
$db = Registry::Get('db');
		$sql = "SELECT id_mf_admin FROM mf_admin WHERE login=:1 AND password=:2";
		$stmt=$db->Prepare($sql);
		$stmt->BindParam(1, $login);
		$stmt->BindParam(2, md5($password));
		$stmt->Execute();

		if($stmt->NumRows() == 1) {

			$sql = "SELECT last_login FROM mf_admin WHERE login =:1";
			$stmt=$db->Prepare($sql)
			->BindParam(1, $login)
			->Execute();
			$row = $stmt->FetchArray();

			$lastLogin = $row['last_login'];

			$sql = "UPDATE mf_admin SET last_login = now() WHERE login =:1";
			$db->Prepare($sql)
			->BindParam(1, $login)
			->Execute();


			$sql = "SELECT id_mf_admin, email, first_name, role FROM mf_admin WHERE login =:1";
			$stmt=$db->Prepare($sql)
			->BindParam(1, $login)
			->Execute();
			$row = $stmt->FetchArray();
			$userId = $row['id_mf_admin'];
			$email = $row['email'];
			$userName = $row['first_name'];
			$role = $row['role'];


			$sql = "SELECT access from mf_admin_access where role=:1";
			$stmt=$db->Prepare($sql)
			->BindParam(1, $role)
			->Execute();

			$access = array();
			while ($row = $stmt->FetchArray()) {
				$access[$row['access']] = '1';
			}

			$admin = new Admin($userId, $email, $userName, $login, $role, $access, $lastLogin);
			SessionProxy::SetValue(EnumSessionValue::ADMIN_OBJECT, $admin);
			return true;
		} else {
			return false;
		}
    }

    /**
     * Zwraca obiekt klasy User lub 0
     *
     * @return boolean
     */
    public static function GetAdmin() {

        $admin = SessionProxy::GetValue(EnumSessionValue::ADMIN_OBJECT);
        $remember = Request::GetCookie('remember');
		//nie zapamietujemy logowania
		$remember = null;
        if(is_object($admin) && $admin->getAuthorized()){
            return $admin;
        } else if(isset($remember) && $remember == '1'){
 	$adminList = AdminDAL::GetResult(array("hash" => Request::GetCookie('key')), array(), 1);

        if(!empty($adminList)) {
		$admin = $adminList[0];

                $admin->SetAuthorized(true);
		$admin->GetAccess();

                SessionProxy::SetValue(EnumSessionValue::ADMIN_OBJECT, $admin);
                return $admin;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    /**
     * Przeladowuje uzytkownika w sesji
     *
     * @param
     */
    public static function ReloadUser() {
        $admin = SessionProxy::GetValue(EnumSessionValue::ADMIN_OBJECT);
        $adminId = $admin->GetId();
        $admin = AdminDAL::GetById($adminId);

	$admin->GetAccess();

        $admin->SetAuthorized(true);
//	$smarty = Registry::Get('smarty');
//	$smarty->assign('admin', $admin);

        SessionProxy::ClearValue(EnumSessionValue::ADMIN_OBJECT);
        SessionProxy::SetValue(EnumSessionValue::ADMIN_OBJECT, $admin);

    }

    /**
     * Czysci sesje wylogowujac usera
     *
     */
    public static function Logout() {
        SessionProxy::ClearValue(EnumSessionValue::ADMIN_OBJECT);
    }
}

?>